﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Timthoy.Ding
 *      Date:           2013/6/27 22:05:07
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.DB;
using Sector7.Medical.EntityType;
using Sector7.Medical.Consts;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class SickBedLevelDAO :DAOBase<M7_SickBedLevel>, ISickBedLevelDAO
    {
        #region Public functions

        /// <summary>
        /// 获取所有病床等级信息
        /// </summary>
        /// <returns></returns>
        public List<M7_SickBedLevel> ReadAll()
        {
            string sql = @"
                SELECT 
                    s.BedLevelId,
                    s.BedLevelName,
                    s.IsValid,
                    s.Price,
                    s.DeptId,
                    s.Lifecycle,
                    s.LifecycleUnitId,
                    de.DeptId,
                    de.DeptName,
                    lu.UnitId,
                    lu.UnitName
                    FROM M7_SickBedLevel s
                        LEFT JOIN  M7_Department de ON s.DeptId = de.DeptId
                        LEFT JOIN  M7_Unit lu ON s.LifecycleUnitId = lu.UnitId";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_SickBedLevel> bedLevelList = new List<M7_SickBedLevel>();

                SqlDataReader reader = helper.ExecuteQuery(sql);
                while (reader.Read())
                {
                    var bedLevel = new M7_SickBedLevel();
                    DBUtil.ReadData(reader, bedLevel);

                    var dept = new M7_Department();
                    DBUtil.ReadData(reader, dept);

                    var unit = new M7_Unit();
                    DBUtil.ReadData(reader, unit);

                    bedLevel.Dept = dept;
                    bedLevel.LifecycleUnit = unit;

                    bedLevelList.Add(bedLevel);
                }

                return bedLevelList;
            }
        }

        /// <summary>
        /// 添加病床等级信息
        /// </summary>
        /// <param name="area">病床等级信息</param>
        /// <returns></returns>
        public override object Create(M7_SickBedLevel bedLevel)
        {
            string sql = @"
                INSERT INTO M7_SickBedLevel
                        (BedLevelName
                        ,Price
                        ,DeptId
                        ,Lifecycle
                        ,IsValid)
                    VALUES
                        (@BedLevelName
                        ,@Price
                        ,@DeptId
                        ,@Lifecycle
                        ,@IsValid)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(bedLevel.BedLevelName, "BedLevelName"),
                    DBUtil.CreateSqlParameter(bedLevel.Price, "Price"),
                    DBUtil.CreateSqlParameter(bedLevel.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(bedLevel.Lifecycle, "Lifecycle"),
                    DBUtil.CreateSqlParameter(bedLevel.IsValid, "IsValid"));
            }
        }
        #endregion

        #region Structure
        public SickBedLevelDAO()
        {
            m_tableName = C_M7_TableName.SICK_BED_LEVEL;
            m_primaryKeyName = C_ColumnName.BED_LEVEL_ID;
        }
        #endregion
    }
}
